package com.xiaoxiao.springbootstudy.controller;

import com.xiaoxiao.springbootstudy.pojo.Customer;
import com.xiaoxiao.springbootstudy.pojo.ScalJh;
import com.xiaoxiao.springbootstudy.service.CustomerService;
import com.xiaoxiao.springbootstudy.service.ScalJhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class SellJhController {
    @Autowired
    private ScalJhService scalJhService;

    @PostMapping("/addSell")
    public Map<String, Object> addSell(@RequestBody ScalJh scalJh) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 400);
        map.put("msg", "操作失败");
        try {
            scalJhService.save(scalJh);
            map.put("code", 200);
            map.put("msg", "OK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @GetMapping("/listSell")
    public Map<String, Object> getSellList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        return scalJhService.getSellList(pageNum, pageSize);
    }

    @PostMapping("/updateSell")
    public Map<String, Object> updateSell(@RequestBody ScalJh sell) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 400);
        map.put("msg", "更新失败");

        try {
            scalJhService.updateById(sell);
            map.put("code", 200);
            map.put("msg", "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }
}
